/*
 * #%L
 * SwiftUICodeGenerator
 * $Id:$
 * $HeadURL:$
 * %%
 * Copyright (C) 2011 - 2012 Linkwithweb
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */
/**
 * 
 */
package com.linkwithweb.products.swiftui.generator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.linkwithweb.products.swiftui.dao.PlatformDAO;
import com.linkwithweb.products.swiftui.spring.jdbc.DatabaseResult;
import com.linkwithweb.products.swiftui.utils.FreeMarkerEngine;
import com.linkwithweb.products.swiftui.utils.ServiceLocator;
import com.linkwithweb.products.swiftui.utils.SpringInitilizer;

/**
 * @author ashwin
 *         Updated ON: Nov 26, 2012 1:44:48 PM
 * 
 */
public class CodeGenerator {
	static {
		SpringInitilizer initilizer = new SpringInitilizer();
	}
	private PlatformDAO platformDAO;

	/**
	 * @return the platformDAO
	 */
	public PlatformDAO getPlatformDAO() {
		if (platformDAO == null) {
			platformDAO = (PlatformDAO) ServiceLocator.getService("platformDAO");
		}
		return platformDAO;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * Get Table Definitions
		 */
		CodeGenerator generator = new CodeGenerator();
		List<String> tableNames = generator.getPlatformDAO().getTableNames("jdbcTemplate");
		for (String tableName : tableNames) {
			//System.out.println(tableName);
			/**
			 * For each table get DB Result
			 */
			String tableQuery = "select * from " + tableName + " where 1=2";
			DatabaseResult result = generator.getPlatformDAO().loadResult(tableQuery, "jdbcTemplate");

			/**
			 * Now merge this with template to generate Code
			 */
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("context", result);
			/**
			 * Generate POJO
			 */
			try {
				mergeAndGenerateCode(params, "c:\\test\\pojos\\" + toProperCase(tableName) + ".java","POJOGenerator.tmpl");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			/**
			 * Generate Row Mapper
			 */
			//mergeAndGenerateCode(params, "c:\\test\\pojos\\" + toProperCase(tableName) + ".java","RowMapperGenerator.tmpl");
		}
	}

	private static String toProperCase(String inputString) {
		StringBuilder ff = new StringBuilder();

		for (String f : inputString.split("_")) {
			if (ff.length() > 0) {
				ff.append(" ");
			}
			ff.append(f.substring(0, 1).toUpperCase()).append(f.substring(1, f.length()).toLowerCase());
		}

		return ff.toString();
	}

	/**
	 * 
	 * @param params
	 * @param fileName 
	 * @return
	 */
	private static void mergeAndGenerateCode(Map<String, Object> params, String fileName,String templateName) {
		/**
		 * The below statement returns merged code .. Store that to given filename..
		 */
		System.out.println(FreeMarkerEngine.getInstance().evaluateTemplate(templateName, params));
	}
}
